package com.pureut.production.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pureut.common.core.web.controller.BaseController;
import com.pureut.common.core.web.domain.AjaxResult;
import com.pureut.common.core.web.page.TableDataInfo;
import com.pureut.production.domain.AmHrGuard;
import com.pureut.production.domain.AmHrSkills;
import com.pureut.production.domain.vo.AmHrGuardVo;
import com.pureut.production.domain.vo.AmHrSkillsVo;
import com.pureut.production.service.AmHrGuardService;
import com.pureut.production.service.AmHrSkillsService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/skills")
public class AmHrSkillsController extends BaseController {
    @Resource
    private AmHrSkillsService amHrSkillsService;


    @GetMapping("/list")
    public TableDataInfo list(AmHrSkillsVo amHrSkillsVo) {
        startPage();
        AmHrSkills amHrSkills = new AmHrSkills();
        amHrSkills.setSklCode(amHrSkills.getSklCode());
        amHrSkills.setSklName(amHrSkills.getSklName());

        LambdaQueryWrapper<AmHrSkills> resultQuery = new LambdaQueryWrapper<AmHrSkills>();
        if(ObjectUtil.isNotNull(amHrSkillsVo.getSklCode())){
            resultQuery.like(AmHrSkills::getSklCode, amHrSkillsVo.getSklCode());
        }
        if(ObjectUtil.isNotNull(amHrSkillsVo.getSklName())){
            resultQuery.like(AmHrSkills::getSklName, amHrSkillsVo.getSklName());
        }

        return getDataTable(amHrSkillsService.list(resultQuery));
    }

    @DeleteMapping("/delete/{guardCode}")
    public AjaxResult remove(@PathVariable("guardCode") Long id) {
        return toAjax(amHrSkillsService.removeById(id));
    }

    @PostMapping("/add")
    public AjaxResult add(@RequestBody AmHrSkillsVo amHrSkillsVo) {
        AmHrSkills amHrSkills = new AmHrSkills();
        amHrSkills.setSklCode(amHrSkillsVo.getSklCode());
        amHrSkills.setSklName(amHrSkillsVo.getSklName());
        amHrSkills.setSklLevel(amHrSkillsVo.getSklLevel());
        amHrSkills.setSklAssesstyle(amHrSkillsVo.getSklAssesstyle());
        amHrSkills.setSklPasscore(new BigDecimal(amHrSkillsVo.getSklPasscore()));
        amHrSkills.setRemark(amHrSkillsVo.getRemark());
        amHrSkills.setCreateTime(new Date());

        return toAjax(amHrSkillsService.save(amHrSkills));
    }

    @GetMapping("/view/{guardCode}")
    public AjaxResult view(@PathVariable("guardCode") Long id) {
        return AjaxResult.success(amHrSkillsService.getById(id));
    }

    @PostMapping("/update")
    public AjaxResult update(@RequestBody AmHrSkills amHrSkills) {
        return toAjax(amHrSkillsService.updateById(amHrSkills));
    }

    @GetMapping("/getSkills")
    public AjaxResult getSkills() {
        return AjaxResult.success(amHrSkillsService.list(new QueryWrapper<AmHrSkills>()
                        .select("distinct skl_code, skl_name"))
                .stream()
                .map(item -> {
                    Map<String, Object> map = new HashMap<>();
                    map.put("sklCode", item.getSklCode());
                    map.put("sklName", item.getSklName());
                    return map;
                }));
    }

}
